package com.haltakov.letterjumble.game.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class Player {
	
	protected String name;
	protected PlayerType type;
	protected int score;
	protected ArrayList<BoardWord> wordsHistory;
	protected ArrayList<Letter> letters;
	protected GameController game;

	protected Player(GameController game, String name, PlayerType type, int score, List<BoardWord> wordHistory) {
		this.name = name;
		this.type = type;
		this.game = game;
		this.score = score;
		
		wordsHistory = new ArrayList<BoardWord>();
		
		if (wordHistory != null)
			wordsHistory.addAll(wordHistory);
		
		letters = new ArrayList<Letter>();
	}

	public List<Letter> getLetters() {
		return Collections.unmodifiableList(letters);
	}

	public GameController getGame() {
		return game;
	}

	public String getName() {
		return name;
	}

	public PlayerType getType() {
		return type;
	}

	public int getScore() {
		return score;
	}

	public List<BoardWord> getWordsHistory() {
		return Collections.unmodifiableList(wordsHistory);
	}
	
	public void addLetter(Letter l) {
		letters.add(l);
	}
	
	public void addWord(BoardWord word) {
		wordsHistory.add(word);
		
		for (Letter l: word.getLetters()) {
			boolean isRemoved = letters.remove(l);
			assert isRemoved;
		}
			
		score += word.getScore();
	}
	
	public int getLetterCount() {
		return letters.size();
	}
	
	abstract public void play();
	abstract public void playAgain();
}
